package org.xxpay.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.xxpay.core.common.constant.PayConstant;
import org.xxpay.core.common.util.MyLog;
import org.xxpay.core.entity.PayCashCollBindRelation;
import org.xxpay.core.entity.PayCashCollBindRelationExample;
import org.xxpay.core.service.IPayCashCollBindRelationService;
import org.xxpay.service.dao.mapper.PayCashCollBindRelationMapper;

import java.util.List;

/**
 * @author: pangxiaoyu
 * @date: 2020/02/17
 */
@Service
public class PayCashCollBindRelationServiceImpl implements IPayCashCollBindRelationService {

    private static final MyLog _log = MyLog.getLog(PayCashCollBindRelationServiceImpl.class);

    @Autowired
    private PayCashCollBindRelationMapper payCashCollBindRelationMapper;

    @Override
    public int addOrUpdate(PayCashCollBindRelation payCashCollBindRelation) {
        PayCashCollBindRelationExample example = new PayCashCollBindRelationExample();
        PayCashCollBindRelationExample.Criteria criteria = example.createCriteria();
        criteria.andAccountEqualTo(payCashCollBindRelation.getAccount());
        criteria.andPassageAccountIdEqualTo(payCashCollBindRelation.getPassageAccountId());
        List<PayCashCollBindRelation> relationList = payCashCollBindRelationMapper.selectByExample(example);
        int result;
        if (relationList.size() != 0) {
            payCashCollBindRelation.setId(relationList.get(0).getId());
            result = payCashCollBindRelationMapper.updateByPrimaryKeySelective(payCashCollBindRelation);
        }else {
            result = payCashCollBindRelationMapper.insertSelective(payCashCollBindRelation);
        }
        return result;
    }

    @Override
    public int update(PayCashCollBindRelation payCashCollBindRelation) {
        return payCashCollBindRelationMapper.updateByPrimaryKey(payCashCollBindRelation);
    }

    @Override
    public boolean isBind(int passageAccountId, String account) {
        PayCashCollBindRelationExample example = new PayCashCollBindRelationExample();
        PayCashCollBindRelationExample.Criteria criteria = example.createCriteria();
        criteria.andAccountEqualTo(account);
        criteria.andPassageAccountIdEqualTo(passageAccountId);
        List<PayCashCollBindRelation> list = payCashCollBindRelationMapper.selectByExample(example);
        if (list.size() == 1) {
            PayCashCollBindRelation bindRelation = list.get(0);
            if (bindRelation.getStatus() == PayConstant.PAY_BIND_RELATION_STATUS_SUCCESS){
                return true;
            }
        }
        return false;
    }

    @Override
    public List<PayCashCollBindRelation> list() {
        PayCashCollBindRelationExample example = new PayCashCollBindRelationExample();
        return payCashCollBindRelationMapper.selectByExample(example);
    }
}
